package com.goods.business.service.imp;

import com.goods.business.converter.OutStockConverter;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.mapper.OutStockMapper;
import com.goods.business.service.ConsumerService;
import com.goods.business.service.OutStockService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.OutStock;
import com.goods.common.vo.business.OutStockVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OutStockServiceImpl implements OutStockService {

    @Autowired
    private OutStockMapper outStockMapper;
    @Autowired
    private ConsumerMapper consumerMapper;

    @Override
    public List<OutStockVO> findOutStockList(Integer pageNum, Integer pageSize,OutStockVO StockVO) {
        if (StockVO.getType()==null&&StockVO.getOutNum()==null&&StockVO.getStatus()==null){
//查询所有
            List<OutStock> outStocks = outStockMapper.selectAll();
            //转化
            List<OutStockVO> outStockVOS = OutStockConverter.converterToOutStocks(outStocks);
            List<OutStockVO> stockVOS = outStockVOS.stream().map(outStockVO -> {
                Long consumerId = outStockVO.getConsumerId();
                //获取consumer
                Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
                //将需要的数据添加到outStockVO中
                outStockVO.setName(consumer.getName());
                outStockVO.setAddress(consumer.getAddress());
                outStockVO.setPhone(consumer.getPhone());
                outStockVO.setSort(consumer.getSort());
                outStockVO.setContact(consumer.getContact());
                return outStockVO;
            }).collect(Collectors.toList());
            return stockVOS;
        }else {
            OutStock outStock = OutStockConverter.converterToStockVo(StockVO);
            Example example = new Example(OutStock.class);
            example.createCriteria()
                    .andEqualTo("outNum",outStock.getOutNum())
                    .andEqualTo("type",outStock.getType())
                    .andEqualTo("status",outStock.getStatus());
            List<OutStock> outStocks = outStockMapper.selectByExample(example);
            List<OutStockVO> outStockVOS = OutStockConverter.converterToOutStocks(outStocks);
            return outStockVOS;
        }

    }

}
